home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / prog / graphics / bin_tree.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  1.6 KB  |  83 lines

  1.  
  2. #include <LEDA/impl/bin_tree.h>
  3. #include <LEDA/impl/avl_tree.h>
  4. #include <LEDA/impl/bb_tree.h>
  5. #include <LEDA/impl/rb_tree.h>
  6. #include <LEDA/impl/rs_tree.h>
  7.  
  8. #include <LEDA/window.h>
  9.  
  10.  
  11. window W;
  12.  
  13. void draw_node(double x, double y, void*, int bal)
  14. { W.draw_node(x,y,blue);
  15.   W.draw_ctext(x,y,string("%d",bal),violet); 
  16.  }
  17.  
  18. void draw_leaf(double x, double y, void* key, int)
  19. { W.draw_ctext(x,y,string("%d",key),black); }
  20.  
  21.  
  22. void draw_edge(double x0, double y0, double x1, double y1)
  23. { W.draw_edge(point(x0,y0),point(x1,y1),blue); }
  24.  
  25.  
  26. main()
  27. {
  28.   bin_tree* TREE[5];
  29.   string    NAME[5];
  30.  
  31.   TREE[0] = new bin_tree;  NAME[0] = "BINARY TREE";
  32.   TREE[1] = new avl_tree;  NAME[1] = "AVL TREE";
  33.   TREE[2] = new bb_tree;  NAME[2] = "BB[ALPHA] TREE";
  34.   TREE[3] = new rb_tree;   NAME[3] = "RED/BLACK TREE";
  35.   TREE[4] = new rs_tree;   NAME[4] = "RANDOMIZED SEARCH TREE";
  36.  
  37.  
  38.   panel P("BINARY TREES");
  39.  
  40.   int n = 20;
  41.   int mode = 0;
  42.   int sel = 0;
  43.  
  44.   P.choice_item("TREE",sel,"bin_tree","avl_tree","bb_tree","rb_tree","rs_tree");
  45.   P.choice_item("INPUT",mode,"random", "1 2 3 ...");
  46.  
  47.   P.int_item("# INSERTS",n,0,50);
  48.  
  49.   W.set_node_width(15);
  50.   //W.set_line_width(2);
  51.   W.set_text_mode(transparent);
  52.  
  53.   for(;;)
  54.   {
  55.     P.open();
  56.  
  57.     W.clear();
  58.     W.message(NAME[sel]);
  59.  
  60.     bin_tree* T = TREE[sel];
  61.   
  62.     T->clear();
  63.   
  64.     init_random();
  65.  
  66.     int i;
  67.   
  68.     if (mode==0)
  69.       for(i=0;i<n;i++) T->insert((void*)random(0,99),0);
  70.     else
  71.       for(i=0;i<n;i++) T->insert((void*)i,0);
  72.   
  73.     double dy = (W.ymax()-W.ymin())/10;
  74.   
  75.     T->draw(draw_node,draw_leaf,draw_edge, W.xmin(),W.xmax(),W.ymax()-dy,dy);
  76.   
  77.     W.read_mouse();
  78.   
  79.   }
  80.   
  81.   return 0;
  82. }
  83.